<?php

namespace app\admin\model;

use think\Model;


class Meetingcontent extends Model
{

    

    

    // 表名
    protected $name = 'meeting_content';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'starttime_text',
        'endtime_text',
        'user_id_text',
        'project_id_text',
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function admin()
    {
        return $this->hasOne('admin','id','user_id');
    }

    public function project()
    {
        return $this->hasOne('project','id','project_id');
    }


    public function getStarttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStarttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
 //修改用户id
 protected function setUserIDAttr($value)
 {
     //有可能是excel提交的，有可能是添加的 都是字符串
     //只有数字和逗号
     $par = '/^[(0-9)(\,)+]+$/';
     if(!preg_match($par, $value)){
         $value = explode(',',$value);
         foreach($value as $v){
             $arr[] = Admin::where('nickname',$v)->value('id');
         }
         return implode(',',$arr);
     }
     return $value;
 }

 public function getUserIDTextAttr($value,$row){

     $arr_value = explode(',',$row['user_id']);
     foreach($arr_value as $v){
         $arr[] = Admin::where('id',$v)->value('nickname');
     }
     return implode(',',$arr);
 }


  //修改用户id
  protected function setProjectIDAttr($value)
  {
      //有可能是excel提交的，有可能是添加的 都是字符串
      //只有数字和逗号
      $par = '/^[(0-9)(\,)+]+$/';
      if(!preg_match($par, $value)){
          $value = explode(',',$value);
          foreach($value as $v){
              $arr[] = Project::where('title',$v)->value('id');
          }
          return implode(',',$arr);
      }
      return $value;
  }

  public function getProjectIDTextAttr($value,$row){

      $arr_value = explode(',',$row['project_id']);
      foreach($arr_value as $v){
          $arr[] = Project::where('id',$v)->value('title');
      }
      return implode(',',$arr);
  }




}
